<?php 
include('../../../../inc/application_settings.php');

$email = $_COOKIE['EMAIL'];
$target = (isset($_REQUEST['T'])) ? $_REQUEST['T'] : null;
$newsId = (isset($_REQUEST['NEWS_ID'])) ? $_REQUEST['NEWS_ID'] : null;
$password = Cryptic::decrypt($_COOKIE['PASSWORD'], $email);
$auth = new Authentication($email, $password);

if ($auth->checkAuthentication()){
	switch ($target) {
		case 'NEW':
			$output = loadForm();
			break;
		case 'EDIT':
			$output = loadForm($newsId);
			break;
	}
} else 
	$output = 'Not authenticated!';
echo $output;


/**
 * generates the output for german and english form to create a news entry
 * encloses the both forms with list for tabs
 * create tab functionality on javascript side
 * @param		int		$newsId
 */
function loadForm($newsId = null){
	$output = '<div id="newsTabs">';
	$output .= loadTabList();
	$output .= loadBasicForm('de', $newsId);
	$output .= loadBasicForm('en', $newsId);
	$output .= '</div>';
	return $output;
}


function loadTabList(){
	$output = <<<TABLIST
	<div class="toolbar tabbar" style="width: 100%">
		<ul class="tabs"> 
		    <li class="first"><a class="selected" href="#news_de">Deutsch</a></li> 
		    <li><a href="#news_en">Englisch</a></li> 
	  	</ul>
  	</div>
TABLIST;
	return $output; 
}


function loadBasicForm($language, $newsId = null){
	if ($newsId != null)
		$news = getNewsData($newsId, $language);
		
	$output = '<div id="news_' . $language . '" style="margin-top: 10px">';
	$output .= '<table border="0">';
	$output .= 		'<tr>';
	$output .= 			'<td><span class="mandatory">Headline:</span></td>';
	$output .= 			'<td><input type="text" id="news_headline_' . $language . '" style="width: 150px" value="' . (($news != null) ? $news['headline_' . $language] : '') . '"/></td>';
	$output .= 			'<td><span class="mandatory">Anzeigen von:</span></td>';
	$output .= 			'<td><input type="text" id="news_display_from_' . $language . '" style="width: 150px" value="' . (($news != null) ? $news['display_from'] : '') . '" onblur="copyText(\'news_display_from_en\', this.value);"/></td>';
	$output .= 		'</tr>';
	$output .= 		'<tr>';
	$output .= 			'<td><span class="mandatory">Kurzinfo:</span></td>';
	$output .= 			'<td><input type="text" id="news_short_info_' . $language . '" style="width: 150px" value="' . (($news != null) ? $news['short_info_' . $language] : '') . '"/></td>';
	$output .= 			'<td><span class="mandatory"</span>Anzeigen bis:</span></td>';
	$output .= 			'<td><input type="text" id="news_display_to_' . $language . '" style="width: 150px" value="' . (($news != null) ? $news['display_to'] : '') . '" onblur="copyText(\'news_display_to_en\', this.value);"/></td>';
	$output .= 		'</tr>';
	$output .= 		'<tr>';
	$output .= 			'<td><span class="mandatory">Info:</span></td>';
	$output .= 			'<td colspan="3"><textarea id="news_long_info_' . $language . '" class="news_long_info" cols="80" rows="10">' . (($news != null) ? $news['long_info_' . $language] : '') . '</textarea></td>';
	$output .= 		'</tr>';
	$output .= '</table>';

	$output .= '</div>';
	return $output;
}


function getNewsData($newsId, $language){
	$newsArr = null;
	
	$sql = 'select headline_' . $language . ', short_info_' . $language . ', ';
	$sql .= 'long_info_' . $language . ', display_from, display_to from ' . DB_BIO . '.news_view where news_id = ?';
	$params = array($newsId);
	
	$sqlObj = Sql_Object::getInstance();
	$sqlObj->connect();
	$sqlObj->execute($sql, $params);
	
	if (($row = $sqlObj->fetch()) != null){
		$newsArr = array();
		foreach($row as $news => $attribute)
			$newsArr[$news] = $attribute;
	}
	
	$sqlObj->disconnect();
	return $newsArr;
}
?>